package com.xique.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xique.common.core.utils.DateUtils;
import com.xique.park.bean.domain.ParkGarageCarSpace;
import com.xique.park.mapper.ParkGarageCarSpaceMapper;
import com.xique.park.service.service.IParkGarageCarSpaceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 车位Service业务层处理
 *
 * @author xique
 * @date 2021-12-09
 */
@Slf4j
@Service
public class ParkGarageCarSpaceServiceImpl implements IParkGarageCarSpaceService {

    @Resource
    private ParkGarageCarSpaceMapper parkGarageCarSpaceMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询车位
     *
     * @param id 车位ID
     * @return 车位
     */
    @Override
    public ParkGarageCarSpace selectParkGarageCarSpaceById(Long id) {
        return parkGarageCarSpaceMapper.selectById(id);
    }

    /**
     * 查询车位列表
     *
     * @param parkGarageCarSpace 车位
     * @return 车位
     */
    @Override
    public List<ParkGarageCarSpace> selectParkGarageCarSpaceList(ParkGarageCarSpace parkGarageCarSpace) {
        QueryWrapper<ParkGarageCarSpace> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id", parkGarageCarSpace.getCarId());
        return parkGarageCarSpaceMapper.selectList(wrapper);
    }

    /**
     * 新增车位
     *
     * @param parkGarageCarSpace 车位
     * @return 结果
     */
    @Override
    public int insertParkGarageCarSpace(ParkGarageCarSpace parkGarageCarSpace) {
        parkGarageCarSpace.setCreateTime(DateUtils.getNowDate());
        return parkGarageCarSpaceMapper.insert(parkGarageCarSpace);
    }

    /**
     * 修改车位
     *
     * @param parkGarageCarSpace 车位
     * @return 结果
     */
    @Override
    public int updateParkGarageCarSpace(ParkGarageCarSpace parkGarageCarSpace) {
        parkGarageCarSpace.setUpdateTime(DateUtils.getNowDate());
        return parkGarageCarSpaceMapper.updateById(parkGarageCarSpace);
    }

    /**
     * 批量删除车位
     *
     * @param ids 需要删除的车位ID
     * @return 结果
     */
    @Override
    public int deleteParkGarageCarSpaceByIds(List<Long> ids) {
        return parkGarageCarSpaceMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByMap(Map<String, Object> map) {
        return parkGarageCarSpaceMapper.deleteByMap(map);
    }

    @Override
    public int batchInsert(List<ParkGarageCarSpace> list) {
        return parkGarageCarSpaceMapper.insertBatch(list);
    }

    @Override
    public int batchUpdate(List<ParkGarageCarSpace> list) {
        return parkGarageCarSpaceMapper.updateBatch(list);
    }

    @Override
    public void batchInsertList(List<ParkGarageCarSpace> parkGarageCarSpaceList) {
        long startTime = System.currentTimeMillis();
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            ParkGarageCarSpaceMapper parkGarageCarSpaceMapper = sqlSession.getMapper(ParkGarageCarSpaceMapper.class);
            parkGarageCarSpaceList.forEach(parkGarageCarSpaceMapper::insert);
            sqlSession.commit();
            sqlSession.clearCache();
        } catch (Exception e) {
            log.info("车位服务区：批量新增异常，异常信息：{}", e.getMessage());
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        log.info("车位服务区：批量新增耗时：{}毫秒", System.currentTimeMillis() - startTime);
    }

    @Override
    public void batchUpdateListById(List<ParkGarageCarSpace> parkGarageCarSpaceList) {
        long startTime = System.currentTimeMillis();
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            ParkGarageCarSpaceMapper parkGarageCarSpaceMapper = sqlSession.getMapper(ParkGarageCarSpaceMapper.class);
            parkGarageCarSpaceList.forEach(parkGarageCarSpaceMapper::updateById);
            sqlSession.commit();
            sqlSession.clearCache();
        } catch (Exception e) {
            log.info("车位服务区：批量更新异常，异常信息：{}", e.getMessage());
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        log.info("车位服务区：批量更新耗时：{}毫秒", System.currentTimeMillis() - startTime);
    }

    @Override
    public List<ParkGarageCarSpace> selectParkGarageCarSpaceByCarId(Long carId) {
        return parkGarageCarSpaceMapper.selectParkGarageCarSpaceByCarId(carId);
    }

}
